import { IChannelListItem, ITplRouteDetailRes, TFormValues } from './interface'

export const textInputFields = ['routeDesc_titleCn', 'routeDesc_descCn', 'routeDesc_titleEn', 'routeDesc_descEn']

export const createInitialFormValues = (data: ITplRouteDetailRes | null): TFormValues => {
  // 默认值尽可能使用空串
  const values: TFormValues = {
    routeId: undefined,
    routeNode: undefined,
    routeNo: '',
    logisticsStatus: undefined,
    routeType: 1,
    errorType: undefined,
    visibleFlag: 1,
    actionFlag: 0,
    routeDesc: {
      descId: undefined,
      titleCn: '',
      titleEn: '',
      descCn: '',
      descEn: '',
    },
    routeChannelList: [],
  }

  if (data) {
    const vals = values as any
    const dat = data as any
    Object.keys(vals).forEach(key => {
      if (dat[key] != null) {
        vals[key] = dat[key] // 赋值
      }
    })
    Object.keys(vals.routeDesc).forEach(key => {
      if (dat.routeDesc[key] != null) {
        vals.routeDesc[key] = dat.routeDesc[key] // 赋值
      }
    })
  }

  return values
}

export const validateRow = (row: IChannelListItem | null | undefined) => {
  if (!row) return false
  const keepDate = row.keepDate
  const keepDateNum = Number(keepDate)
  return /^\d+$/.test(keepDate) && 0 < keepDateNum && keepDateNum < 10000
}
